package com.sskj.mine.ui.fragment;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.RecordsBean;
import com.sskj.mine.presenter.AssetPresenter;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/18
 */
@Route(path = RConfig.MINE_FRAGMENT_ASSET)
public class AssetFragment extends BaseFragment<AssetPresenter> {


    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter slimAdapter;
    //  0 提币 1 充币
    @Autowired
    int type;
    private MVCCoolHelper<List<RecordsBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.mine_fragment_asset;
    }

    @Override
    public AssetPresenter getPresenter() {
        return new AssetPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        HashMap<String, String> tibiMap = new HashMap<>();
        tibiMap.put("1", App.INSTANCE.getString(R.string.mine_assetFragment1_j));
        tibiMap.put("2", App.INSTANCE.getString(R.string.mine_assetFragment2_j));
        tibiMap.put("3", App.INSTANCE.getString(R.string.mine_assetFragment3_j));
        tibiMap.put("4", App.INSTANCE.getString(R.string.mine_assetFragment4_j));
        tibiMap.put("5", App.INSTANCE.getString(R.string.mine_assetFragment5_j));
        HashMap<String, String> chongbiMap = new HashMap<>();
        chongbiMap.put("1", App.INSTANCE.getString(R.string.mine_assetFragment6_j));
        chongbiMap.put("2", App.INSTANCE.getString(R.string.mine_assetFragment7_j));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.mine_BEC))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.mine_item_asset_records, new SlimInjector<RecordsBean>() {
            @Override
            public void onInject(RecordsBean data, IViewInjector injector, List<Object> list) {

                injector
                        .text(R.id.item_address_title, type == 0 ? App.INSTANCE.getString(R.string.mine_assetFragment8_j) : App.INSTANCE.getString(R.string.mine_assetFragment9_j))
                        .text(R.id.item_num_title, type == 0 ? App.INSTANCE.getString(R.string.mine_assetFragment10_j) : App.INSTANCE.getString(R.string.mine_assetFragment11_j))
                        .text(R.id.tvTime, type == 0 ? App.INSTANCE.getString(R.string.mine_assetFragment12_j) : App.INSTANCE.getString(R.string.mine_assetFragment13_j))
                        .visibility(R.id.llCheck, type == 0 ? View.VISIBLE : View.GONE)
                        .visibility(R.id.tvCancel, (type == 0 && data.getState().equals("1")) ? View.VISIBLE : View.GONE)
                        .text(R.id.item_address, data.getQianbao_url())
                        .text(R.id.item_num, data.getPrice() + data.getPname())
                        .clicked(R.id.tvCancel, v -> {
                            TipUtil.getSureCancelTip(getActivity(), App.INSTANCE.getString(R.string.mine_mineLanguageActivity2_j), App.INSTANCE.getString(R.string.mine_assetFragment15_j), () -> {
                                mPresenter.cancelTibi(data.getId());
                            });
                        })
                        .text(R.id.item_time, type == 0 ? data.getAddtime() : chongbiMap.get(data.getState()));
                if (type == 0) {
                    injector.text(R.id.item_time2, data.getCheck_time());
                    injector.text(R.id.item_status, tibiMap.get(data.getState()))
                            .textColor(R.id.item_status, ContextCompat.getColor(App.INSTANCE, data.getState().equals("5") ? R.color.mine_0A1 : R.color.mine_0A1));
                }
            }

        }).attachTo(recyclerView);
        slimAdapter.updateData(new ArrayList());
        ModelRx2DataSource<RecordsBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<RecordsBean>)
                page -> mPresenter.getAssetRecord((type == 0 ? "cash" : "recharge"), page + "", "20"), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    public void cancelSuccess() {
        mvcHelper.refresh();
    }
}
